package com.osmp.web.user.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.osmp.web.user.entity.SysPrivilege;
import com.osmp.web.user.service.SysPrivilegeService;

/**
 * Description:
 * 
 * @author: wangkaiping
 * @date: 2014年10月13日 上午11:22:39
 */
@Controller
@RequestMapping("/privilege")
public class SysPrivilegeController {

	@Autowired
	private SysPrivilegeService services;

	@RequestMapping("/toList")
	public String list() {
		return "user/privilege/list";
	}

	@RequestMapping("/toAdd")
	public String toAdd() {
		return "user/privilege/privilegeAdd";
	}

	@RequestMapping("/toEdit")
	public String toEdit(SysPrivilege p, HttpServletRequest req) {
		SysPrivilege sysPrivilege = services.get(p);
		req.setAttribute("privilege", sysPrivilege);
		return "user/privilege/privilegeEdit";
	}

	@RequestMapping("/addPrivilege")
	@ResponseBody
	public Map<String, Object> addPrivilege(SysPrivilege p) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (null != p && p.getId() != 0) {
				services.update(p);
			} else {
				services.insert(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", "操作失败");
			return map;
		}
		map.put("success", true);
		map.put("msg", "操作成功");
		map.put("obj", p);
		return map;
	}

	@RequestMapping("/deletePrivilege")
	@ResponseBody
	public Map<String, Object> deletePrivilege(SysPrivilege p) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			services.delete(p);
		} catch (Exception e) {
			map.put("success", false);
			map.put("msg", "操作失败");
			return map;
		}
		map.put("success", true);
		map.put("msg", "操作成功");
		return map;
	}

	
}
